package com.jdk.demo.thread.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class AllOfDemo2 {
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(2);
			}catch (InterruptedException e) {
				throw new IllegalStateException(e);
			}
			return "result of f1";
		});
		CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
			}catch (InterruptedException e) {
				throw new IllegalStateException(e);
			}
			return "result of f2";
		});
		CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(3);
			}catch (InterruptedException e) {
				throw new IllegalStateException(e);
			}
			return "result of f3";
		});
		CompletableFuture<Void> anyOfFuture = CompletableFuture.allOf(future1, future2, future3);
		System.out.println(anyOfFuture.get());
	}

}
